import * as vscode from 'vscode';
import { basename, dirname, join, relative } from 'path';

interface IEntry {
  name: string;
  type: string;
}

export interface FtpNode {

  resource: vscode.Uri;
  isDirectory: boolean;

}

export class DataModel {
  constructor(readonly host: string, private user: string, private password: string) {
  }

  // public get roots(): Thenable<FtpNode[]> {
  // 	return this.connect().then(client => {
  // 		return new Promise((c, e) => {
  // 			client.list((err, list) => {
  // 				if (err) {
  // 					return e(err);
  // 				}

  // 				client.end();

  // 				return c(this.sort(list.map(entry => ({ resource: vscode.Uri.parse(`ftp://${this.host}///${entry.name}`), isDirectory: entry.type === 'd' }))));
  // 			});
  // 		});
  // 	});
  // }

  // public getChildren(node: FtpNode): Thenable<FtpNode[]> {
  // 	return this.connect().then(client => {
  // 		return new Promise((c, e) => {
  // 			client.list(node.resource.fsPath, (err, list) => {
  // 				if (err) {
  // 					return e(err);
  // 				}

  // 				client.end();

  // 				return c(this.sort(list.map(entry => ({ resource: vscode.Uri.parse(`${node.resource.fsPath}/${entry.name}`), isDirectory: entry.type === 'd' }))));
  // 			});
  // 		});
  // 	});
  // }

  // private sort(nodes: FtpNode[]): FtpNode[] {
  // 	return nodes.sort((n1, n2) => {
  // 		if (n1.isDirectory && !n2.isDirectory) {
  // 			return -1;
  // 		}

  // 		if (!n1.isDirectory && n2.isDirectory) {
  // 			return 1;
  // 		}

  // 		return basename(n1.resource.fsPath).localeCompare(basename(n2.resource.fsPath));
  // 	});
  // }

  // public getContent(resource: vscode.Uri): Thenable<string> {
  // 	return this.connect().then(client => {
  // 		return new Promise((c, e) => {
  // 			client.get(resource.path.substr(2), (err, stream) => {
  // 				if (err) {
  // 					return e(err);
  // 				}

  // 				let string = '';
  // 				stream.on('data', function (buffer) {
  // 					if (buffer) {
  // 						const part = buffer.toString();
  // 						string += part;
  // 					}
  // 				});

  // 				stream.on('end', function () {
  // 					client.end();
  // 					c(string);
  // 				});
  // 			});
  // 		});
  // 	});
  // }
}

export class DataTreeDataProvider implements vscode.TreeDataProvider<string>{

  datas: Array<string> = [];
  rootPath: string = "";
  private _onDidChangeTreeData: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
  readonly onDidChangeTreeData: vscode.Event<any> = this._onDidChangeTreeData.event;
  constructor(private readonly model?: DataModel) { }


  public refresh(): any {
    this._onDidChangeTreeData.fire(undefined);
  }

  public setData(datas: Array<string>, rootPath: string) {
    this.datas = datas;
    this.rootPath = rootPath;
  }

  public getTreeItem(element: string): vscode.TreeItem {
    const label = relative(this.rootPath, element);
    return {
      resourceUri: vscode.Uri.parse(element),
      collapsibleState: void 0,
      label: label,
      command: {
        command: 'relyon.openFile',
        arguments: [vscode.Uri.parse(element)],
        title: 'Open OnRely File'
      }
    };
  }

  public getChildren(element?: string): string[] | Thenable<string[]> {
    return element ? [] : this.datas;
  }

  public getParent(element: string): string | undefined {
    return undefined;
  }

}